package com.example.edianzu.somur.utils.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

import java.io.IOException;

/**
 * author：风不会停息 on 2017/7/22 18:43
 * mailbox：yh131412hys@163.com
 * project：Somur
 * tel：13295355900
 * describe：
 */

public class CustomService extends Service {

    private MediaPlayer mediaPlayer;
    private String tempPath;
    private String netStateType;
    private boolean isConnected = false;

    @Override
    public void onCreate() {
        super.onCreate();
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
        }
    }

    /**
     * 绑定服务后再startService(),只会调用该方法
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 退出程序时，释放mediaPlayer
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new CriFMServiceBinder();
    }

    /**
     * 获取网络类型
     *
     * @return
     */
    private String getNetStateType() {
        return netStateType;
    }

    /**
     * 是否联网
     *
     * @return
     */
    private boolean isConnectedNet() {
        return isConnected;
    }


    /**
     * 播放
     *
     * @param path
     */
    private void play(String path) {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        } else {
            try {
                Toast.makeText(this, "调用播放逻辑", Toast.LENGTH_SHORT).show();
                mediaPlayer.setDataSource(path);
                mediaPlayer.prepareAsync();
//                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//                    @Override
//                    public void onPrepared(MediaPlayer mp) {
//                        mp.start();
//                    }
//                });
//                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
//                    @Override
//                    public void onCompletion(MediaPlayer mp) {
//                        mp.start();
//                    }
//                });
                mediaPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    /**
     * 中间人，通过它回调service的方法
     */
    private class CriFMServiceBinder extends Binder implements CustomCaller {


        @Override
        public void callPlay(String path) {
            play(path);
        }

        @Override
        public void callSeekToPosition(int position) {

        }

        @Override
        public boolean callIsConnected() {
            return isConnectedNet();
        }

        @Override
        public String callGetNetStateType() {
            return getNetStateType();
        }


    }
}
